let request = require('../libs/request')
let {resolve} = require('path')
let JSDOM = require('jsdom').JSDOM
let Mysql = require('mysql-pro')
let gbk = require('gbk')

// 链接Mysql, 注意格式出入的是一个json, json当中key为mysql, value又是一个json
let db = new Mysql({
    mysql: {
        host: 'localhost',
        port: 3306,
        user: 'root',
        password: '123456',
        database: 'tmall_shouji'
    }
})

// 封装mysql的执行函数, db.starTransaction()...调用方法
async function query(sql) {
    console.log(sql)
    await db.startTransaction()
    let data = await db.executeTransaction(sql)
    await db.stopTransaction()
    
    return data
}

// 解析器
function html2$(html) {
    let document = new JSDOM(html).window.document
    return document.querySelectorAll.bind(document)
}

// 页面解析
function indexParser(buffer) {
    let $ = html2$(html2$(buffer.toString())('textarea.f1')[0].value)
    return Array.from($('li')).map(li => {
        let oA = li.getElementsByClassName('mod-g-photo')[0]
        return {
            url: 'http:' + oA.href,
            img_src: 'http:' + oA.children[0].getAttribute('data-lazyload-src'),
            name: li.getElementsByClassName('mod-g-tit')[0].children[0].text,
            description: li.getElementsByClassName('mod-g-desc')[0].innerHTML,
            // price: li.getElementsByClassName('mod-g-nprice')[0].innerHTML.match(/\d+(\.\d+)?/g)[0],
            sales: li.getElementsByClassName('mod-g-sales')[0].innerHTML.match(/(\d+)/)[0]
        }
    })
}

// 页面爬虫
async function indexSpider() {
    try {
        let {body, headers} = await request('https://shouji.tmall.com/')
        let data = indexParser(body)
        indexProcessor(data)
    } catch(e) {
        console.log('indexSpider请求失败', e)
    }
}

// 数据处理函数
async function indexProcessor(data) {
    // 存入数据库
    // for (let i=0; i<data.length; i++) {
    //     let rows = await query(`SELECT * FROM item_table WHERE url='${data[i].url}'`)

    //     if (rows.length > 0) {
    //         await query(`UPDATE item_table SET img_src='${data[i].img_src}', name='${data[i].name}', description='${data[i].description}', sales='${data[i].sales}' WHERE ID=${rows[0].ID}`)
    //     } else {
    //         await query(`INSERT INTO item_table (ID, url, img_src, name, description, sales) VALUES(0, '${data[i].url}', '${data[i].img_src}', '${data[i].name}', '${data[i].description}', '${data[i].sales}')`)
    //     }
    // }

    // 继续爬取数据
    for (let i=0; i<data.length; i++) {
        await detailSpider(data[i].url)
    }
}

async function detailSpider(url) {
    try {
        let {body, headers} = await request(url)
        
        let datas = await detailParser(body)
        await detailProcessor(datas)
    } catch(e) {
        console.log('detail请求失败', e)
    }
}

function detailParser(body) {
    let $ = html2$(gbk.toString('utf-8', body))

    let attributes = {}
    Array.from($('.attributes-list li')).forEach(li => {
        let n = li.innerHTML.search(/:|： /)
        if (n == -1) return
        let key = li.innerHTML.substring(0, n)
        let value = li.innerHTML.substring(n+1)

        attributes[key] = value

        console.log(attributes)
    })
}

async function detailProcessor(data) {

}


;(async () => {
    await indexSpider()
    await detailProcessor()
})()